package icu.feigroup.domain.activity.service.trial.node;

import icu.feigroup.domain.activity.model.entity.MarketProductEntity;
import icu.feigroup.domain.activity.model.entity.TrialBalanceEntity;
import icu.feigroup.domain.activity.model.valobj.GroupActivityDiscountVO;
import icu.feigroup.domain.activity.service.trial.AbstractGroupBuyMarketSupport;
import icu.feigroup.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import icu.feigroup.types.design.framwork.tree.StrategyHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * projectName:icu.feigroup.domain.activity.service.trial.node
 *
 * @author 飞
 * @since 2025/2/21 17:26
 * description:
 */
@Slf4j
@Service
public class TagNode extends AbstractGroupBuyMarketSupport<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> {
    @Resource
    private EndNode endNode;
    @Override
    protected TrialBalanceEntity doApply(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        GroupActivityDiscountVO groupActivityDiscountVO = dynamicContext.getGroupActivityDiscountVO();
        String tagId = groupActivityDiscountVO.getTagId();
        boolean visible = groupActivityDiscountVO.isVisible();
        boolean enable = groupActivityDiscountVO.isEnable();
        if (StringUtils.isBlank(tagId)){
            dynamicContext.setVisible(true);
            dynamicContext.setEnable(true);
            return router(requestParameter,dynamicContext);
        }
        boolean isWithin = repository.isTagCrowdRange(tagId,requestParameter.getUserId());
        dynamicContext.setVisible(visible || isWithin);
        dynamicContext.setEnable(enable || isWithin);
        return router(requestParameter,dynamicContext);
    }

    @Override
    public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        return endNode;
    }
}
